// Vue路由配置文件
// 数字档案管理系统前端路由配置

import Vue from 'vue';
import Router from 'vue-router';

// 导入页面组件
import Login from '../views/Login.vue';
import Register from '../views/Register.vue';
import DashboardLayout from '../layout/Layout.vue';
import Home from '../views/Home.vue';
import UserPermission from '../views/UserPermission.vue';
import ArchiveReceiveDigitize from '../views/ArchiveReceiveDigitize.vue';
import CategoryManagement from '../views/CategoryManagement.vue';
import TagManagement from '../views/TagManagement.vue';
import ArchiveStorageRetrieval from '../views/ArchiveStorageRetrieval.vue';
import ArchiveConfidentialityAccess from '../views/ArchiveConfidentialityAccess.vue';
import PersonalArchives from '../views/PersonalArchives.vue';
import ArchiveConfidentialityAdjustment from '../views/ArchiveConfidentialityAdjustment.vue';
import ArchiveAppraisalDestruction from '../views/ArchiveAppraisalDestruction.vue';
import ArchiveApprovalRequestSubmit from '../views/ArchiveApprovalRequestSubmit.vue';
import ArchiveApprovalRequestManagement from '../views/ArchiveApprovalRequestManagement.vue';
// 新增功能模块组件
import NoticeManagement from '../views/NoticeManagement.vue';
import TrainingManagement from '../views/TrainingManagement.vue';
import ArchiveModificationManagement from '../views/ArchiveModificationManagement.vue';
import Dashboard from '../views/Dashboard.vue';
import SystemLogs from '../views/SystemLogs.vue';
import OCRTest from '../views/OCRTest.vue';

// 注册Vue Router插件
Vue.use(Router);

// 创建路由实例
const router = new Router({
  mode: 'history',  // 使用HTML5 History模式，去掉URL中的#
  base: '/',        // 应用的基路径
  routes: [
    // 登录页面路由
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    // 注册页面路由
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    // 主应用布局路由，包含所有业务页面
    {
      path: '/',
      component: DashboardLayout,
      children: [
        // 首页
        {
          path: '',
          name: 'Home',
          component: Home
        },
        // 数据可视化与报表
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: Dashboard,
          meta: { title: '数据可视化与报表' }
        },
        // 用户与权限管理
        {
          path: 'user-permission',
          name: 'UserPermission',
          component: UserPermission,
          meta: { title: '用户与权限管理' }
        },
        // 档案生命周期管理 - 档案接收与数字化
        {
          path: 'archive-lifecycle/receive-digitize',
          name: 'ArchiveReceiveDigitize',
          component: ArchiveReceiveDigitize,
          meta: { title: '档案接收与数字化' }
        },
        // 档案生命周期管理 - 分类管理
        {
          path: 'archive-lifecycle/classify-tag/categories',
          name: 'CategoryManagement',
          component: CategoryManagement,
          meta: { title: '分类管理' }
        },
        // 档案生命周期管理 - 标签管理
        {
          path: 'archive-lifecycle/classify-tag/tags',
          name: 'TagManagement',
          component: TagManagement,
          meta: { title: '标签管理' }
        },
        // 档案生命周期管理 - 档案存储与检索
        {
          path: 'archive-lifecycle/storage-retrieval',
          name: 'ArchiveStorageRetrieval',
          component: ArchiveStorageRetrieval,
          meta: { title: '档案存储与检索' }
        },
        // 档案生命周期管理 - 档案密级与访问控制
        {
          path: 'archive-lifecycle/confidentiality-access',
          name: 'ArchiveConfidentialityAccess',
          component: ArchiveConfidentialityAccess,
          meta: { title: '档案密级与访问控制' }
        },
        // 档案生命周期管理 - 档案鉴定与销毁
        {
          path: 'archive-lifecycle/appraisal-destruction',
          name: 'ArchiveAppraisalDestruction',
          component: ArchiveAppraisalDestruction,
          meta: { title: '档案鉴定与销毁' }
        },
        // 档案审批管理 - 提交档案审批请求
        {
          path: 'archive-approval/submit',
          name: 'ArchiveApprovalRequestSubmit',
          component: ArchiveApprovalRequestSubmit,
          meta: { title: '提交档案审批请求' }
        },
        // 档案审批管理 - 档案审批请求管理
        {
          path: 'archive-approval/management',
          name: 'ArchiveApprovalRequestManagement',
          component: () => import('@/views/ArchiveApprovalRequestManagement.vue'),
          meta: { title: '档案审批请求管理' }
        },
        // 受限档案申请管理
        {
          path: '/restricted-archive-request',
          name: 'RestrictedArchiveRequest',
          component: () => import('@/views/RestrictedArchiveRequest.vue')
        },
        {
          path: '/physical-archive-retrieval',
          name: 'PhysicalArchiveRetrieval',
          component: () => import('@/views/PhysicalArchiveRetrieval.vue')
        },
        // 企业公告管理
        {
          path: 'notice-management',
          name: 'NoticeManagement',
          component: NoticeManagement,
          meta: { title: '企业公告管理' }
        },
        // 培训资料管理
        {
          path: 'training-management',
          name: 'TrainingManagement',
          component: TrainingManagement,
          meta: { title: '培训资料管理' }
        },
        // 档案修改流程管理
        {
          path: 'archive-modification-management',
          name: 'ArchiveModificationManagement',
          component: ArchiveModificationManagement,
          meta: { title: '档案修改流程管理' }
        },
        // 系统日志与审计
        {
          path: 'system-logs',
          name: 'SystemLogs',
          component: SystemLogs,
          meta: { title: '系统日志与审计' }
        },
        // OCR文字识别测试
        {
          path: 'ocr-test',
          name: 'OCRTest',
          component: OCRTest,
          meta: { title: 'OCR文字识别测试' }
        },
        // 新增个人档案查看路由
        {
          path: 'archive-lifecycle/personal-archives',
          name: 'PersonalArchives',
          component: PersonalArchives,
          meta: { title: '个人档案查看' }
        },
        // 新增档案密级调整申请路由
        {
          path: 'archive-lifecycle/confidentiality-adjustment',
          name: 'ArchiveConfidentialityAdjustment',
          component: ArchiveConfidentialityAdjustment,
          meta: { title: '档案密级调整申请' }
        }
      ]
    }
  ]
});

// 路由守卫：登录状态检查
router.beforeEach((to, from, next) => {
  // 从localStorage获取用户token
  const token = localStorage.getItem('token');
  
  if (to.path !== '/login' && to.path !== '/register' && !token) {
    // 如果没有token且不是登录/注册页，则跳转到登录页
    next('/login');
  } else if ((to.path === '/login' || to.path === '/register') && token) {
    // 如果有token且访问登录/注册页，则跳转到首页
    next('/');
  } else {
    // 正常跳转
    next();
  }
});

export default router; 